﻿using RpcCacheClient.Interface;
using RpcClient.Interface;
using RpcHelper;
using RpcSync.DAL;
using RpcSync.Model;

namespace RpcSync.Collect.Collect
{
    internal class RemoteServerGroupCollect : IRemoteServerGroupCollect
    {
        private readonly IUnityCollect _Unity;
        private readonly ILocalCacheController _Cache;
        public RemoteServerGroupCollect(IUnityCollect unity, ILocalCacheController cache)
        {
            this._Unity = unity;
            this._Cache = cache;
        }

        public MerServer[] GetAllServer(long merId)
        {
            string key = string.Concat("Server_", merId);
            if (this._Cache.TryGet(key, out MerServer[] servers))
            {
                return servers;
            }
            servers = this._Unity.Resolve<IRemoteServerGroupDAL>().GetAllServer(merId);
            this._Cache.Set(key, servers);
            return servers;
        }

        public void Refresh(long merId)
        {
            string key = string.Concat("Server_", merId);
            this._Cache.Remove(key);
        }
    }
}
